package cn.edu.gmall.realtime.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateTimeUtil {
    private final static DateTimeFormatter formator = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat yMdDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat yMdHmsDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String toyMdHms(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        return formator.format(localDateTime);
    }

    public static String toyMdHms(Long ts) {
        return yMdHmsDateFormat.format(ts);
    }

    public static String toyMd(Long ts) {
        return yMdDateFormat.format(ts);
    }

    public static synchronized Long toTs(String yMdHms) {
        Long ts = null;
        try {
            ts = yMdHmsDateFormat.parse(yMdHms).getTime();
        } catch (ParseException e) {
            System.out.println(yMdHms + ".toTs() error");
            e.printStackTrace();
        }
        return ts;
    }

    public static void main(String[] args) throws ParseException {
        String date = "2021-12-27 01:47:42";

        System.out.println(DateTimeUtil.toTs(date));
    }
}
